﻿using System;
using Microsoft.Office.Interop.Excel;

namespace MobileLoader
{
    public class ExcelHelper
    {
        public static string GetCellValueAsString(Worksheet worksheet, int rowIndex, int columnIndex)
        {            
            var range = (Range)worksheet.Cells[rowIndex, columnIndex];
            return range.Value2 == null ? "" : range.Value2.ToString();
        }

        public static double GetCellValueAsDouble(Worksheet worksheet, int rowIndex, int columnIndex)
        {
            var range = (Range)worksheet.Cells[rowIndex, columnIndex];
            object value = range.Value2;

            if (value == null)
                return 0;
            try
            {
                return Double.Parse(value.ToString());
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static DateTime GetCellValueAsDateTime(Worksheet worksheet, int rowIndex, int columnIndex)
        {
            var range = (Range)worksheet.Cells[rowIndex, columnIndex];
            object value = range.Value2;

            if (value == null)
                return DateTime.MinValue;
            try
            {
                double date = GetCellValueAsDouble(worksheet, rowIndex, columnIndex);
                return DateTime.FromOADate(date);
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
        }

    }
}
